import axios from "axios"
import auth from "./auth"
import qs from "qs"


const SERVER_HOST = "http://127.0.0.1:5000"
// 封装网络请求库
class Http {
    //构造函数
    constructor() {
        if (process.env.NODE_ENV == "production") {
            this.server_host = window.location.origin
        } else {
            this.server_host="http://127.0.0.1:5000"
        }
        
        this.http = axios.create({
            baseURL: SERVER_HOST + "/cmsapi",
            timeout: 1000 * 60
        });
        //请求之前的拦截，设置JWT
        this.http.interceptors.request.use(config => {
            const token = auth.token
            if (token) {
                config.headers.Authorization = "Bearer " + token
            }
            return config
        })
        //响应后的拦截
        this.http.interceptors.response.use(response => {
            return response.data;
        })

    }
    _post(url, data) {
        return this.http.post(url, qs.stringify(data));
    }
    //添加轮播图
    addbanner(data) {
        const url = "/banner/add/";
        return this._post(url, data);
    }
    //获取轮播图数据列表
    getbannerlist() {
        const url = "/banner/list/";
        return this.http.get(url);
    }
    //删除轮播图
    delbanner(id) {
        const url = "/banner/delete/"
        return this._post(url, { 'id': id })
    }
    //编辑轮播图
    editbanner(data) {
        const url = "/banner/edit/"
        return this._post(url, data)
    }

    // 帖子列表
    getpostlist(page) {
        const url = "/post/list/?page=" + (page ? page : 1)
        return this.http.get(url);
    }

    //删除帖子
    deletepost(post_id) {
        const url = "/post/delete/"
        return this._post(url, { 'id': post_id })
    }

    //评论列表
    getcommentlist(page) {
        const url = "/comment/list/?page=" + (page ? page : 1)
        return this.http.get(url);
    }

    //删除评论
    deletecomment(comment_id) {
        const url = "/comment/delete/"
        return this._post(url, { "id": comment_id })
    }

    //用户列表
    getuserlist(page) {
        const url = "/user/list/?page=" + (page ? page : 1)
        return this.http.get(url);
    }
    //设置用户状态
    setuserstatus(user_id, is_active) {
        const url = "/user/active/"
        return this._post(url, { "id": user_id, "is_active": is_active })
    }
    //删除用户
    deleteuserinfo(user_id) {
        const url = "/user/delete/"
        return this._post(url, { "id": user_id })
    }
    //获取板块帖子数量
    GetPostCountForBoard() {
        const url = "/board/post/count/"
        return this.http.get(url)
    }
    //获取最近7天帖子数量
    GETLatestPostCount() {
        const url = "/latest/post/count/"
        return this.http.get(url)
    }


}
export default new Http()